function MainAssistant() {
}

MainAssistant.prototype.setup = function() {
	// no focus on anything at start
	this.controller.setInitialFocusedElement(null);
	
	// spinner
    this.spinner = this.controller.get('spin-scrim');
    this.spinnerAttrs = {
        	spinnerSize: 'large'
    	}
    this.spinnerModel = {
            spinning: false
    	}; 
    this.controller.setupWidget('feedSpinner', this.spinnerAttrs, this.spinnerModel);
	this.spinner.hide();
	
	// create menu    
    this.appMenuAttr = {omitDefaultItems: true};
	this.appMenuModel = {
            visible: true,
            items: [
				Mojo.Menu.editItem,
                { label: 'About...', command: 'do-about' }
            ]
        };
	this.controller.setupWidget(Mojo.Menu.appMenu, this.appMenuAttr, this.appMenuModel);
	
	// choose input file button
	this.controller.setupWidget("chooseInputFileButton",
        this.chooseInputFileButtonAttributes = {
            },
        this.chooseInputFileButtonModel = {
            label : "Open Images",
            disabled: false
        });
	Mojo.Event.listen(this.controller.get("chooseInputFileButton"), Mojo.Event.tap, this.choose.bindAsEventListener(this));
	
	// text fiel for input file
	this.inputFileTextFieldAttributes = {
			hintText: $L('Please select image ...'),
            multiline: true,
            enterSubmits: false,
            focus: false
	};
	this.inputFileTextFieldModel = {
			value: '',
			disabled: true
	};
	this.controller.setupWidget('inputFileTextField', this.inputFileTextFieldAttributes, this.inputFileTextFieldModel);

	// text field for output file
	this.outputFileTextFieldAttributes = {
			hintText: $L('Output image ...'),
            multiline: true,
            enterSubmits: false,
            focus: false,
			changeOnKeyPress: true
	};
	this.outputFileTextFieldModel = {
			value: '',
			disabled: false
	};
	this.controller.setupWidget('outputFileTextField', this.outputFileTextFieldAttributes, this.outputFileTextFieldModel);
	Mojo.Event.listen(this.controller.get("outputFileTextField"), Mojo.Event.propertyChange, this.checkResizeButton.bind(this));
		
	// text field for output width
	this.outputFileWidthTextFieldAttributes = {
			hintText: $L(''),
            multiline: false,
            enterSubmits: false,
			maxLength: 10,
			charsAllow: this.inputAllowOnlyNumbers.bind(this),
            focus: false,
			changeOnKeyPress: true
	};
	this.outputFileWidthTextFieldModel = {
			value: '',
			disabled: false
	};
	this.controller.setupWidget('outputFileWidthTextField', this.outputFileWidthTextFieldAttributes, this.outputFileWidthTextFieldModel);
	Mojo.Event.listen(this.controller.get("outputFileWidthTextField"), Mojo.Event.propertyChange, this.recalcImageSize.bind(this));
		
	// text field for output width
	this.outputFileHeightTextFieldAttributes = {
			hintText: $L(''),
            multiline: false,
            enterSubmits: false,
			maxLength: 10,
			charsAllow: this.inputAllowOnlyNumbers.bind(this),
            focus: false,
			changeOnKeyPress: true
	};
	this.outputFileHeightTextFieldModel = {
			value: '',
			disabled: false
	};
	this.controller.setupWidget('outputFileHeightTextField', this.outputFileHeightTextFieldAttributes, this.outputFileHeightTextFieldModel);
	Mojo.Event.listen(this.controller.get("outputFileHeightTextField"), Mojo.Event.propertyChange, this.recalcImageSize.bind(this));

	// resize button
	this.resizeButtonModel = {
	    label : "Resize",
	    disabled: true
	}
	this.controller.setupWidget("resizeButton", {}, this.resizeButtonModel);
	Mojo.Event.listen(this.controller.get("resizeButton"), Mojo.Event.tap, this.resize.bind(this));
	
}

MainAssistant.prototype.recalcImageSize = function(event) {
	//Mojo.Controller.errorDialog("Bla: " + event.model);

	if ((this.controller.get("inputFileWidthLabel").innerText == "") ||
			(this.controller.get("inputFileHeightLabel").innerText == ""))
		// no input data
		return;

	var inputWidth = parseInt(this.controller.get("inputFileWidthLabel").innerText);
	var inputHeight = parseInt(this.controller.get("inputFileHeightLabel").innerText);
	var aspecRatio = inputWidth / inputHeight;

	if (event.model == this.outputFileWidthTextFieldModel) {
		//Mojo.Controller.errorDialog("Width: " + event.model);
		var width = parseInt(this.outputFileWidthTextFieldModel.value);
		this.outputFileHeightTextFieldModel.value = Math.round(width / aspecRatio);
		this.controller.modelChanged(this.outputFileHeightTextFieldModel);
	}
	else if (event.model == this.outputFileHeightTextFieldModel) {
		//Mojo.Controller.errorDialog("Height: " + event.model);
		var height = parseInt(this.outputFileHeightTextFieldModel.value);
		this.outputFileWidthTextFieldModel.value = Math.round(height * aspecRatio);
		this.controller.modelChanged(this.outputFileWidthTextFieldModel);
	}

	this.checkResizeButton();
}

MainAssistant.prototype.inputAllowOnlyNumbers = function(inputChar) {
	if ((inputChar >= 48) && (inputChar <= 57))
		return true;
	return false;
}

MainAssistant.prototype.choose = function(event) {
	//Mojo.Controller.errorDialog("Blabla");

	var params = { kinds: ['image'] , onSelect: this.imageSelectCB.bind(this) }
	Mojo.FilePicker.pickFile(params, this.controller.stageController);
}

MainAssistant.prototype.myVersionCallback = function(result){
	Mojo.Controller.errorDialog("Success: " + result.returnValue + ":" + result.height);
}

MainAssistant.prototype.myVersionFailureCallback = function(result){
	Mojo.Controller.errorDialog("Error: " + result);
}

MainAssistant.prototype.imageSelectCB = function(file) {
	this.inputFile = file.fullPath;

	// set label with input file	
	this.inputFileTextFieldModel.value = this.inputFile;
	this.controller.modelChanged(this.inputFileTextFieldModel);
	
	this.controller.serviceRequest('palm://com.tippix.imagepresizer', {
			method: "identify",
			parameters: {
				input: this.inputFile
			},
			onSuccess: this.myIdentifyCallback.bind(this),
			onFailure: this.myIdentifyFailureCallback.bind(this)
		});
}

MainAssistant.prototype.byteToKilobyte = function(input) {
	var result = parseFloat(input);
	result /= 1024;
	result *= 100;
	result = Math.round(result) / 100;
	return result;
}

MainAssistant.prototype.myIdentifyCallback = function(result) {
	// set width and height
	this.controller.get("inputFileWidthLabel").innerText = result.width;
	this.controller.get("inputFileHeightLabel").innerText = result.height;
	this.controller.get("inputFileSizeLabel").innerText = this.byteToKilobyte(result.size) + " KB";
	
	// set predefined output file
	var outputFile = this.inputFile.substring(0, this.inputFile.lastIndexOf('.')) + "_resized.jpg";
	this.outputFileTextFieldModel.value = outputFile;
	this.controller.modelChanged(this.outputFileTextFieldModel);
	
	// set predefined output size
	this.outputFileWidthTextFieldModel.value = result.width;
	this.controller.modelChanged(this.outputFileWidthTextFieldModel);
	this.outputFileHeightTextFieldModel.value = result.height;
	this.controller.modelChanged(this.outputFileHeightTextFieldModel);
	
	this.controller.get("outputFileSizeLabel").innerText = "";
	
	this.checkResizeButton();
}

MainAssistant.prototype.myIdentifyFailureCallback = function(result) {
	this.inputFileTextFieldModel.value = "Error open input file.";
	this.controller.modelChanged(this.inputFileTextFieldModel);
}

MainAssistant.prototype.checkResizeButton = function() {
	if ((this.outputFileWidthTextFieldModel.value == "") ||
			(this.outputFileHeightTextFieldModel.value == "") ||
			(this.outputFileTextFieldModel.value == "")) {
		this.resizeButtonModel.disabled = true;	
	}
	else {
		this.resizeButtonModel.disabled = false;
	}
	this.controller.modelChanged(this.resizeButtonModel);
}

MainAssistant.prototype.resize = function(event) {
	//Mojo.Controller.errorDialog("Blabla: " + this.outputFileWidthTextFieldModel.value);
	
	this.spinner.show();
	this.spinnerModel.spinning = true;
	this.controller.modelChanged(this.spinnerModel, this);
	
	//TODO: input and output file with "" for use with spaces
	this.controller.serviceRequest('palm://com.tippix.imagepresizer', {
			method: "resize",
			parameters: {
				input:	this.inputFileTextFieldModel.value,
				output:	this.outputFileTextFieldModel.value,
				width:	this.outputFileWidthTextFieldModel.value,
				height:	this.outputFileHeightTextFieldModel.value
			},
			onSuccess: this.myResizeCallback.bind(this),
			onFailure: this.myResizeFailureCallback.bind(this)
		});
}

MainAssistant.prototype.myResizeCallback = function(result) {
	this.controller.showAlertDialog({
	    onChoose: this.getSecondSize.bind(this),
	    title: $L("Success"),
	    message: $L("Image successfully resized."),
	    choices:[
	        	{label:$L('Ok'), value:"Ok", type:'affirmative'}  
	    	]
    });
}

MainAssistant.prototype.myOutputIdentifyCallback = function(result){
	this.controller.get("outputFileSizeLabel").innerText = this.byteToKilobyte(result.size) + " KB";
	this.spinner.hide();
	this.spinnerModel.spinning = false;
	this.controller.modelChanged(this.spinnerModel, this);
}

MainAssistant.prototype.getSecondSize = function() {
	this.controller.serviceRequest('palm://com.tippix.imagepresizer', {
		method: "identify",
		parameters: {
			input: this.outputFileTextFieldModel.value
		},
		onSuccess: this.myOutputIdentifyCallback.bind(this),
		onFailure: this.myOutputIdentifyFailureCallback.bind(this)
	});
}

MainAssistant.prototype.myOutputIdentifyFailureCallback = function(result){
	Mojo.Controller.errorDialog("Bla: " + event.model);
	
	this.spinner.hide();
	this.spinnerModel.spinning = false;
	this.controller.modelChanged(this.spinnerModel, this);
}

MainAssistant.prototype.myResizeFailureCallback = function(result){
	Mojo.Controller.errorDialog("Error resize image.");

	this.spinner.hide();
	this.spinnerModel.spinning = false;
	this.controller.modelChanged(this.spinnerModel, this);
}

MainAssistant.prototype.activate = function(event) {
}

MainAssistant.prototype.deactivate = function(event) {
}

MainAssistant.prototype.cleanup = function(event) {
	Mojo.Event.stopListening('chooseInputFileButton', Mojo.Event.tap, this.choose);
	Mojo.Event.stopListening('resizeButton', Mojo.Event.tap, this.resize);
	Mojo.Event.stopListening('outputFileWidthTextField', Mojo.Event.propertyChange, this.recalcImageSize);
	Mojo.Event.stopListening('outputFileHeightTextField', Mojo.Event.propertyChange, this.recalcImageSize);
}

MainAssistant.prototype.handleCommand = function(event) {
	if (event.type == Mojo.Event.command) {
		this.cmd = event.command;

		switch(this.cmd) {
			
            case 'do-about':
                this.controller.showAlertDialog({
                    onChoose: function(value) {},
                    title: $L("ImagePREsizer " + Mojo.Controller.appInfo.version),
                    message: $L("Copyright 2009, Uwe Mannl"),
                    choices:[
                        {label:$L("OK"), value:""}
                    ]
                });
                break;
            
			default:
				break;

		} //switch
	} //if
}
